package cn.bblocks.cacheTest.suport.annotations;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * @program:
 * @author: captain.ma
 * @date: 2018-11-20
 * @since:
 */
@Retention(RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Dao {

    String value() default "";

    IgnoreValue ignore() default IgnoreValue.NONE;

    Symbol symbol() default Symbol.EQ;

    //


    public enum IgnoreValue {
        NONE(Objects::isNull),NEVER(o->false), DEFAULT((o) -> true), ZERO((o) -> Integer.parseInt(o + "") == 0), EMPTY(
                (o) -> {
                    Collection<?> c = (Collection<?>) o;
                    if (c != null && c.size() <= 0) {
                        return true;
                    }
                    return false;
                });

        private final Predicate<Object> predicate;

        private IgnoreValue(Predicate<Object> tPredicate) {
            this.predicate = tPredicate;
        }

        public Predicate<Object> getPredicate() {
            return predicate;
        }
    }

    @AllArgsConstructor
    @Getter
    public enum Symbol {

        EQ(" =:", s -> s), LIKE(" like ", s -> "CONCAT('%',:" + s + ",'%')"),
        LEFT_LIKE(" like ", s -> "CONCAT('%',:" + s + ")"), RIGHT_LIKE(" like ", s -> "CONCAT(:" + s + ",'%')"),
        BETWEEN(" BETWEEN ", s -> ":start" + humpPropertyName(s) + " AND :end" + humpPropertyName(s));

        private final String value;

        private final Function<String, String> mapping;

        private static String humpPropertyName(String propertyName) {
            return propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        }

    }
}
